﻿
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.XT;
using Rison.BusinessFacade.Facade.XT;
using Rison.BusinessFacade.Facade.Biz;
namespace Rison.Web.ascx
{
    public partial class InfoContation3 : System.Web.UI.UserControl
    {
        //在调用页面的Page_Init时传入该值
        public int tableWidth = 240;
        public int tableHeight = 210;

        public bool displayDate = false;//是否显示日期
        public int displayRows = 6;

        public int startModuleIndex = 1;
        private int displayModuleNum = 2;
        private int imageItemDisplayRows = 4;//与图片平行的位置显示的信息行数
        private int contentItemDisplayRows = 8;//图片下方位置显示的信息行数

        protected string UrlBase
        {
            get { return PageGlobalParams.PageUrlBase; }
        }

        protected string titleWidth
        {
            get { return (tableWidth - 70 - 18).ToString() + "px"; }
        }

        protected string contentRowHeitht
        {
            get { return (tableHeight - 39).ToString() + "px"; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindModule();
            }
        }

        private void BindModule()
        {
            DataSet modules = new SysModuleFacade().GetHomePageModules();
            if (modules != null)
            {
                DataTable modules2 = modules.Tables[0].Clone();

                for (int i = startModuleIndex; i < (startModuleIndex + displayModuleNum); i++)
                {
                    if (modules.Tables[0].Rows.Count >= i)
                        modules2.ImportRow(modules.Tables[0].Rows[i - 1]);
                }

                modules = null;

                this.dlInfos.DataSource = modules2;
                this.dlInfos.DataBind();
            }
        }

        protected void dlInfos_OnItemDataBound(object sender, DataListItemEventArgs e)
        {
            bool read = false;
            DataRow frameImgInfoRow = null;

            foreach (Control c in e.Item.Controls)
            {
                if (c.GetType().Name == "GridView")
                {
                    if (!displayDate)
                        (c as GridView).Columns[2].Visible = false;

                    BindInfos(c as GridView, Convert.ToInt32(this.dlInfos.DataKeys[e.Item.ItemIndex]));
                    //BindInfos(c as GridView);
                }
                else if (c.GetType().Name == "Image")
                {
                    if (frameImgInfoRow == null && !read)
                    {
                        read = true; frameImgInfoRow = GetFrameImgInfo(Convert.ToInt32(this.dlInfos.DataKeys[e.Item.ItemIndex]));
                    }

                    if (frameImgInfoRow != null)
                    {
                        if (frameImgInfoRow["ImgUrl"].ToString().StartsWith("/"))
                            (c as Image).ImageUrl = PageGlobalParams.PageUrlBase2 + frameImgInfoRow["ImgUrl"].ToString();
                        else
                            (c as Image).ImageUrl = PageGlobalParams.PageUrlBase + frameImgInfoRow["ImgUrl"].ToString();

                        (c as Image).AlternateText = frameImgInfoRow["subject"].ToString();
                        (c as Image).Attributes.Add("OnClick", "javascript:location.href='" + UrlBase + "view/InfoBrowseView.aspx?infoId=" + frameImgInfoRow["infoId"].ToString() + "&mid=" + this.dlInfos.DataKeys[e.Item.ItemIndex].ToString() + "';");
                    }
                }
                else if (c.GetType().Name == "Label" && (c as Label).ID.IndexOf("ImgSubject") > 0)
                {
                    if (frameImgInfoRow == null && !read)
                    {
                        read = true; frameImgInfoRow = GetFrameImgInfo(Convert.ToInt32(this.dlInfos.DataKeys[e.Item.ItemIndex]));
                    }
                    if (frameImgInfoRow != null)
                        if (frameImgInfoRow["subject"].ToString().Length > 7)
                            (c as Label).Text = "<a href='" + UrlBase + "view/InfoBrowseView.aspx?infoId=" + frameImgInfoRow["infoId"].ToString() + "&mid=" + this.dlInfos.DataKeys[e.Item.ItemIndex].ToString() + "'>" + frameImgInfoRow["subject"].ToString().Substring(0, 7) + "</a>";
                        else
                            (c as Label).Text = "<a href='" + UrlBase + "view/InfoBrowseView.aspx?infoId=" + frameImgInfoRow["infoId"].ToString() + "&mid=" + this.dlInfos.DataKeys[e.Item.ItemIndex].ToString() + "'>" + frameImgInfoRow["subject"].ToString() + "</a>";
                }
            }
        }

        private DataRow GetFrameImgInfo(int moduleId)
        {
            DataTable frameImgInfo = new InfomationFacade().GetTopNumInfos(moduleId, 1, true).Tables[0];
            if (frameImgInfo.Rows.Count > 0)
            {
                return frameImgInfo.Rows[0];
            }
            return null;
        }

        private void BindInfos(GridView gvInfo,int moduleId)
        {
            DataSet infos = null;
            InfomationFacade facade = new InfomationFacade();

            if (gvInfo.ID.IndexOf("gvInfos1") >= 0)//图片平行的信息行
            {
                infos = facade.GetTopNumInfos(moduleId, imageItemDisplayRows, false);
            }
            else
            {
                infos = facade.GetTopNumInfos(moduleId, imageItemDisplayRows + contentItemDisplayRows, false);
                for (int i = 0; i < imageItemDisplayRows; i++)
                {
                    if (infos.Tables[0].Rows.Count > 0)
                        infos.Tables[0].Rows.RemoveAt(0);//去掉图片位置的信息行
                }
            }

            gvInfo.DataSource = infos;
            gvInfo.DataBind();

            facade = null;
        }
    }
}